package and.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import and.bean.BaseBean;
import and.common.MyCommon;
import and.process.QueryProcess;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;

import com.gfan.sdk.statitistics.GFAgent;

public class QueryActivity extends ListActivity {
	
	@Override
	protected void onPause() {
		GFAgent.onPause(this);
		super.onPause();
	}
	private QueryProcess queryProcess =new QueryProcess();
	private ListAdapter adapter;
	private Handler handler = new Handler();
	private ProgressDialog progressDialog;
	private int[] baseIntArray = {R.id.num1,R.id.num2,R.id.num3,R.id.num4,R.id.num5,R.id.num6,R.id.num7,R.id.num8};
	private String[] nums;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    
    private String[] createStrArray(){
    	String[] result  =new String[nums.length+2];
    	result[0] = "date";
    	result[1] = "qishu";
    	for(int j=0;j<nums.length;j++){
    		result[j+2] ="num"+j;
		}
    	return result;
    }
    
    @Override
    protected void onResume() {
    	query();
    	GFAgent.onResume(this);
    	super.onResume();
    }
    
    private void query(){
    	 setListAdapter(null);
    	 progressDialog = ProgressDialog.show(this,MyCommon.QUERYACTION+MyCommon.QueryName,"获取数据中……",true);
    	 new Thread(new Runnable(){
    		public void run() {
    			 List<Map<String,String>> datas =new ArrayList<Map<String,String>>();
    	         Map<String,String> data = new HashMap<String,String>();
    	         try{
    	         	List<BaseBean> list =queryProcess.query();
    	         	BaseBean base =list.get(0); ;
    	         	for(int i=0;i<list.size();i++){
    	         		data = new HashMap<String,String>();
    	         		base = list.get(i);
    	         		data.put("date","开奖时间："+base.getDate()+" "+MyCommon.CPTime);
    	         		data.put("qishu","第"+base.getQishu()+"期");
    	         		nums =base.getNum().split(" ");
    	         		for(int j=0;j<nums.length;j++){
    	         			data.put("num"+j, nums[j]);
    	         		}
    	             	datas.add(data);
    	         	}
    	         	adapter =new SimpleAdapter(QueryActivity.this,datas,MyCommon.QueryLayoutId,createStrArray(),createIntArray());
    	         	
    	         	setMyAdapter();
    	         	progressDialog.dismiss();
    	         	
    	         }catch (Exception e) {
    	 			e.printStackTrace();
    	 		}
    		}
    	 }).start();
    	 
        
    }
    private void setMyAdapter(){
    	handler.post(new Runnable() {
    		public void run() {
    			//将获取到的数据更新到列表中
    			setListAdapter(adapter);
    			getListView().setCacheColorHint(Color.TRANSPARENT);
    		}
    	});
    }
    private int[] createIntArray(){
    	int[] result  =new int[nums.length+2];
    	result[0] = R.id.kaijiangDate;
    	result[1] = R.id.qishu;
    	for(int j=0;j<nums.length;j++){
    		result[j+2] = baseIntArray[j];
		}
    	return result;
    }


}